﻿package com.misc {
	import flash.utils.describeType;
	public class Utils {
		public static  function print_r(o:*,name:String="",recur:int=0):String {
			var result:String="";
			var type:String=typeof o;
			var desc:XML=describeType(o);
			// meta data of object
			if (recur == 0) {
				result+= "---------------------------------------------------------------------\n";
			} else {
				for (var i:int=0; i < recur; ++i) {
					result+= "  ";
				}
			}
			if (name) {
				result+= '[' + name + ']';
			}
			result+= ':' + desc.@name + ' => ';
			// content of object
			switch (type) {
				case "boolean" :
				case "number" :
				case "string" :
					result+= String(o);
					break;
				case "xml" :
					result+= o.toXMLString();
					break;
					0;
				case "object" :
					if (desc.@name == "Object" || desc.@name == "Array") {
						for (var key:String in o) {
							result+= "\n";
							result+= print_r(o[key],key,recur + 1);
						}
					} else {
						var prop:XML;
						//trace("UTIL: " + typeof(o));
						//trace("UTIL: " + desc.toXMLString());
						// -- properties
						for each (prop in desc.variable) {
							result+= "\n";
							result+= print_r(o[prop.@name],prop.@name,recur + 1);
						}
						// -- methods
						for each (prop in desc.method) {
							//trace("UTIL: " + prop.toXMLString());
							result+= "\n";
							result+= print_r(o[prop.@name],prop.@name,recur + 1);
						}
						// -- constructor
						//<constructor>
						//  <parameter index="1" type="flash.net::URLRequest" optional="true"/>
						//</constructor>
						// -- inheritance and implements
						//<implementsInterface type="flash.events::IEventDispatcher"/>
						//<extendsClass type="flash.events::EventDispatcher"/>
						//<extendsClass type="Object"/>
						// -- events and others..
						//<metadata name="Event">
						//  <arg key="name" value="httpStatus"/>
						//  <arg key="type" value="flash.events.HTTPStatusEvent"/>
						//</metadata>
					}
					break;
				case "function" :
					//trace("UTIL: " + typeof(o));
					//trace("UTIL: " + desc.toXMLString());
					result+= String(o);
					break;
			}
			if (recur == 0) {
				result+= "\n---------------------------------------------------------------------";
			}
			return result;
		}
		public static  function toCamelCase(str:String):String {
			var r:String="";
			var a:Array=str.split(' ');
			for (var s:String in a) {
				r+= Utils.capitalize(a[s]);
			}
			return r;
		}
		public static  function capitalize(str:String):String {
			var r:String="";
			r+= str.slice(0,1).toUpperCase();
			r+= str.slice(1);
			return r;
		}
	}
}